﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarioArena.World;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MarioArena.Backup
{
    class AiAgent
    {
        private Player mPlayer;
        private Point mLastTileIndex;
        /// <summary>
        /// Grid containing the indexes of the tiles in TileEngine which surround the player.
        /// </summary>
        private Point[,] mIndexGrid;

        Texture2D mDummyTexture;

        public AiAgent(Player player)
        {
            this.mPlayer = player;
            this.mLastTileIndex = player.Character.TileIndex;

            // Create the grid
            this.mIndexGrid = new Point[21, 21]; // 21 x 21 grid surrouding the player
            //UpdateGrid();

            this.mDummyTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            this.mDummyTexture.SetData(new Color[] { Color.White });
        }

        public void Update()
        {
            if (this.mPlayer.Character.TileIndex != this.mLastTileIndex) // Tile index has changed
            {
                UpdateGrid();
                this.mLastTileIndex = this.mPlayer.Character.TileIndex;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            DrawPathfindingGrid(spriteBatch);
        }

        private void DrawPathfindingGrid(SpriteBatch spriteBatch)
        {
            Tile[,] allTiles = TileEngine.Instance.Tiles;
            for (int x = 0; x < this.mIndexGrid.GetLength(0); x++)
            {
                for (int y = 0; y < this.mIndexGrid.GetLength(1); y++)
                {
                    Point tileIndex = this.mIndexGrid[x, y]; // Tile index in the TileEngine grid
                    if (tileIndex.X >= 0 && tileIndex.Y >= 0) // If tile index not out of bounds (because of EmptyGrid)
                    {
                        Tile tile = allTiles[tileIndex.X, tileIndex.Y];
                        Rectangle tileBounds = new Rectangle(tileIndex.X * 24 - Camera.X, tileIndex.Y * 24 - Camera.Y, 24, 24);
                        if (tile.Texture == null)
                            spriteBatch.Draw(this.mDummyTexture, tileBounds, Color.DarkGreen * 0.2f);
                        else spriteBatch.Draw(this.mDummyTexture, tileBounds, Color.Red * 0.5f);
                    }
                }
            }
        }

        private void UpdateGrid()
        {
            EmptyGrid();

            int left = this.mPlayer.Character.TileIndex.X - 10;
            int top = this.mPlayer.Character.TileIndex.Y - 10;
            int right = this.mPlayer.Character.TileIndex.X + 10;
            int bottom = this.mPlayer.Character.TileIndex.Y + 10;
            if (left < 0)
                left = 0;
            if (top < 0)
                top = 0;
            if (right > TileEngine.Instance.LevelWidth - 1)
                right = TileEngine.Instance.LevelWidth - 1;
            if (bottom > TileEngine.Instance.LevelHeight - 1)
                bottom = TileEngine.Instance.LevelHeight - 1;
            Point gridTopLeftIndex = new Point(left, top);
            Point gridBottomRightIndex = new Point(right, bottom);
            Console.WriteLine("Updating grid:\nleft=" + left + " right=" + right + " top=" + top + "bottom=" + bottom);

            int i = 0;
            for (int x = left; x <= right; x++)
            {
                int j = 0;
                for (int y = top; y <= bottom; y++)
                {
                    //Tile tile = TileEngine.Instance.Tiles[x, y];
                    //this.mGrid[i, j] = tile;
                    this.mIndexGrid[i, j] = new Point(x, y); // Store the tile's index in this grid
                    j++;
                }
                i++;
            }
        }

        /// <summary>
        /// Empties the grid containing the tile index to check pathfinding with.
        /// </summary>
        private void EmptyGrid()
        {
            for (int x = 0; x < this.mIndexGrid.GetLength(0); x++)
            {
                for (int y = 0; y < this.mIndexGrid.GetLength(1); y++)
                {
                    // Indices less than zero are not checked nor drawn, because they
                    // are located outside of the arena
                    this.mIndexGrid[x, y] = new Point(-1, -1);
                }
            }
        }
    }
}
